package com.crm.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.service.CountService;
import com.crm.service.CustomerService;
import com.crm.service.impl.CountServiceImpl;
import com.crm.service.impl.CustomerServiceImpl;

public class CountServlet extends HttpServlet {
	CustomerService customerService = new CustomerServiceImpl();
	CountService countServie = new CountServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String flag = req.getParameter("flag");
		if(flag.equals("industry")) {
			industry(req,resp);
		}
		if(flag.equals("level")) {
			level(req,resp);
		}
		if(flag.equals("source")) {
			source(req,resp);
		}
	}

	private void source(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String field = "cust_source";
		List<Map> source = countServie.queryCount(field);
		String[] xValue = new String[source.size()-1];
		String[] yValue = new String[source.size()-1];
		for(int i=0;i<source.size()-1;i++) {
			String str = "'"+source.get(i).get(field).toString()+"'";
			xValue[i] = str;
			yValue[i] = source.get(i).get("count").toString();
		}
		req.setAttribute("xValue", Arrays.toString(xValue));
		req.setAttribute("yValue", Arrays.toString(yValue));
		req.setAttribute("source", source);
		req.getRequestDispatcher("customer_statistical_analysis/source_list.jsp").forward(req, resp);
	}

	private void level(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String field = "cust_level";
		List<Map> level = countServie.queryCount(field);
		String[] xValue = new String[level.size()-1];
		String[] yValue = new String[level.size()-1];
		for(int i=0;i<level.size()-1;i++) {
			String str = "'"+level.get(i).get(field).toString()+"'";
			xValue[i] = str;
			yValue[i] = level.get(i).get("count").toString();
		}
		req.setAttribute("xValue", Arrays.toString(xValue));
		req.setAttribute("yValue", Arrays.toString(yValue));
		req.setAttribute("level", level);
		req.getRequestDispatcher("customer_statistical_analysis/level_list.jsp").forward(req, resp);
	}

	private void industry(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String field = "cust_industry";
		List<Map> industry = countServie.queryCount(field);
		String[] xValue = new String[industry.size()-1];
		String[] yValue = new String[industry.size()-1];
		for(int i=0;i<industry.size()-1;i++) {
			String str = "'"+industry.get(i).get(field).toString()+"'";
			xValue[i] = str;
			yValue[i] = industry.get(i).get("count").toString();
		}
		req.setAttribute("xValue", Arrays.toString(xValue));
		req.setAttribute("yValue", Arrays.toString(yValue));
		req.setAttribute("industry", industry);
		req.getRequestDispatcher("customer_statistical_analysis/industry_list.jsp").forward(req, resp);
	}

	

}
